package com.qingyun.form.base

import android.content.Context
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlin.math.min

class FormLayoutManager(
    context: Context,
    private val formGroup: FormGroup,
) : LinearLayoutManager(context) {
    /** 标题最大宽度占比 */
    var maxTitleWidthRatio = 0.5f
    private var recyclerView: RecyclerView? = null
    private var isInLayoutChildren: Boolean = false
    private var maxTitleWidth = -1

    override fun onAttachedToWindow(view: RecyclerView?) {
        super.onAttachedToWindow(view)
        recyclerView = view
    }

    override fun onDetachedFromWindow(view: RecyclerView?, recycler: RecyclerView.Recycler?) {
        super.onDetachedFromWindow(view, recycler)
        recyclerView = null
    }

    override fun measureChildWithMargins(child: View, widthUsed: Int, heightUsed: Int) {
        super.measureChildWithMargins(child, widthUsed, heightUsed)
        if (isInLayoutChildren) {
            return
        }
        val holder = getChildViewHolder(child)
        val position = holder.adapterPosition
        if (position == RecyclerView.NO_POSITION) {
            return
        }
        calcTitleMaxWidth()
        formGroup[position].layout(holder)
    }

    private fun calcTitleMaxWidth() {
        val count = childCount
        for (i in 0 until count) {
            val childView = findViewByPosition(i) ?: continue
            val holder = getChildViewHolder(childView)
            val position = holder.adapterPosition
            if (position == RecyclerView.NO_POSITION) {
                continue
            }
            val item = formGroup[position]
            val childTitleWidth = item.getTitleLayoutWidth(holder)
            if (childTitleWidth > maxTitleWidth) {
                // 默认 Title布局的宽度最大不能超过父布局的一半
                maxTitleWidth = min((width * maxTitleWidthRatio).toInt(), childTitleWidth)
            }
        }
        formGroup.maxTitleWidth = maxTitleWidth
    }

    override fun onLayoutChildren(recycler: RecyclerView.Recycler?, state: RecyclerView.State?) {
        isInLayoutChildren = true
        super.onLayoutChildren(recycler, state)
        calcTitleMaxWidth()
        layoutChildren()
        isInLayoutChildren = false
    }

    private fun layoutChildren() {
        val count = childCount
        for (i in 0 until count) {
            val childView = findViewByPosition(i) ?: continue
            val holder = getChildViewHolder(childView)
            val position = holder.adapterPosition
            if (position == RecyclerView.NO_POSITION) {
                continue
            }
            holder.maxTitleLayoutWidth = maxTitleWidth
            formGroup[position].layout(holder)
        }
    }

    private fun getChildViewHolder(view: View): FormViewHolder {
        return recyclerView!!.getChildViewHolder(view) as FormViewHolder
    }

    override fun onFocusSearchFailed(
        focused: View,
        focusDirection: Int,
        recycler: RecyclerView.Recycler,
        state: RecyclerView.State
    ): View? {
        val holderView = super.onFocusSearchFailed(focused, focusDirection, recycler, state)?: return null
        val lp = holderView.layoutParams
        if (lp !is RecyclerView.LayoutParams) {
            return holderView
        }
        val position = lp.viewAdapterPosition
        if (position == RecyclerView.NO_POSITION) {
            return holderView
        }
        val item = formGroup[position]
        return item.focusSearch(getChildViewHolder(holderView))
    }
}